package com.dartsstats;

import java.util.List;

import com.dartsstats.R;
import com.dartsstats.R.id;
import com.dartsstats.R.layout;
import com.dartsstats.R.menu;
import com.dartsstats.datasource.TeamDataSource;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

public class EditTeamsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_teams);
		addTeamsToSpinner();
	}

	private void addTeamsToSpinner() {
		Spinner spinner = (Spinner) findViewById(R.id.teamName);
		List<Team> teamList = new TeamDataSource(this).getAllTeams();
//		List<String> teamNames = new ArrayList<String>();
//		for(Team team : teamList) {
//			teamNames.add(team.getName());
//		}
		ArrayAdapter<Team> adapter = new ArrayAdapter<Team>(this,
				android.R.layout.simple_spinner_item, teamList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_edit_teams, menu);
		return true;
	}
	
	public void addNewTeam(View view) {
		TeamDataSource teamDataSource = new TeamDataSource(this);
		EditText textView = (EditText) findViewById(R.id.newTeamName);
		String newTeamName = textView.getText().toString();
		teamDataSource.createTeam(newTeamName, false);
		finish();
		startActivity(getIntent());
	}
	
	public void editTeam(View view) {
		Intent intent = new Intent(this, PlayerListActivity.class);
		Spinner teamSpinner = (Spinner) findViewById(R.id.teamName);
		Team team = (Team) teamSpinner.getSelectedItem();
		long teamID = team.getId();
		intent.putExtra("TEAM_ID", teamID);
    	startActivity(intent);
	}
	
	public void editTeamName(View view) {
		Spinner teamSpinner = (Spinner) findViewById(R.id.teamName);
		Team team = (Team) teamSpinner.getSelectedItem();
		
		LayoutInflater factory = LayoutInflater.from(this);
		final View inputView = factory.inflate(R.layout.activity_edit_team_popup, null);
		
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Edit team");
		alert.setMessage("Edit the team info:");
		
		alert.setView(inputView);
		final EditText input = (EditText) inputView.findViewById(R.id.popupTeamName);
		input.setText(team.getName());
		final CheckBox checkBox = (CheckBox) inputView.findViewById(R.id.isManagedCheckbox);
		checkBox.setChecked(team.isManaged());

		final long teamID = team.getId();

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			Editable text = input.getText();
			TeamDataSource teamDataSource = new TeamDataSource(getBaseContext());
			teamDataSource.updateTeamName(text.toString(),checkBox.isChecked(),teamID);
			startActivity(getIntent());
		  }
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		    //cancel
		  }
		});

		alert.show();
	}

}
